package com.css.fxfzypg.zcpt.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.RedisUtils;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Dbj
 * @date 2022年08月10日  11:17
 */
@RestController
@RequestMapping("/fxfz/sArea")
@Slf4j
public class SysAreaController {
    @Autowired
    private SysAreaService sysAreaService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    /**
     * 获取区域树
     */
    @GetMapping("/jsonSAreaTree")
    public RestResponse jsonSAreaTree(@RequestParam("divisionId") String divisionId) {
        RestResponse restResponse = null;
        try{
            String jsonStr = sysAreaService.jsonSAreaTree(divisionId);
            Gson gson = CreateGson.createGson();
            List sysMenuList = gson.fromJson(jsonStr, List.class);
            restResponse = RestResponse.succeed(sysMenuList);
        }catch (Exception e){
            log.error("查询区域失败!",e);
            restResponse = RestResponse.fail("查询区域失败!");
        }
        return restResponse;
    }

    /**
     * 根据上级区域获取下级区域
     */
    @GetMapping("/getChildAreaById")
    public RestResponse getChildAreaById(@RequestParam("divisionId") String divisionId) {
        RestResponse restResponse = null;
        try{
            String jsonStr = sysAreaService.getChildAreaById(divisionId);
            Gson gson = CreateGson.createGson();
            List sysMenuList = gson.fromJson(jsonStr, List.class);
            restResponse = RestResponse.succeed(sysMenuList);
        }catch (Exception e){
            log.error("查询区域失败!",e);
            restResponse = RestResponse.fail("查询区域失败!");
        }
        return restResponse;
    }

    /**
     * 获取所有的区域code
     */
    @GetMapping("/getAllDivisionCode")
    public RestResponse getAllDivisionCode() {
        Gson gson = CreateGson.createGson();
        RestResponse restResponse = null;
        try{
            List sysAreaList = sysAreaService.getAllDivisionCode();
            restResponse = RestResponse.succeed(sysAreaList);
        }catch (Exception e){
            log.error("查询区域编码失败!",e);
            restResponse = RestResponse.fail("查询区域编码失败!");
        }
        return restResponse;
    }

    /**
     * 获取区域code
     */
    @GetMapping("/getDivisionCodeById")
    public RestResponse getDivisionCodeById(@RequestParam("divisionId") String divisionId) {
        Gson gson = CreateGson.createGson();
        RestResponse restResponse = null;
        try{
            String areaId = sysAreaService.getDivisionCodeById(divisionId);
            areaId =(String) JSONObject.parse(areaId);
            restResponse = RestResponse.succeed(areaId);
        }catch (Exception e){
            log.error("查询区域编码失败!",e);
            restResponse = RestResponse.fail("查询区域编码失败!");
        }
        return restResponse;
    }

    /**
     * 根据省市县名称查询id用于app端在地图上创建数据时保存id的情况
     */
    @GetMapping("/getIdByCondition")
    public RestResponse getIdByCondition(@RequestParam("province") String province,@RequestParam("city") String city,
                                         @RequestParam("area") String area) {
        Gson gson = CreateGson.createGson();
        RestResponse restResponse = null;
        try{
            province = URLEncoder.encode(province,"UTF-8");
            city = URLEncoder.encode(city,"UTF-8");
            area = URLEncoder.encode(area,"UTF-8");
            String jsonStr = sysAreaService.getIdByCondition(province,city,area);
            restResponse = RestResponse.succeed(jsonStr);
        }catch (Exception e){
            log.error("查询区域编码失败!",e);
            restResponse = RestResponse.fail("查询区域编码失败!");
        }
        return restResponse;
    }

}
